package com.suse.lhw.hms.exersiseserver.service.Impl;

import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.suse.lhw.hms.exersiseserver.constants.ConstEMmInstance;
import com.suse.lhw.hms.exersiseserver.constants.ConstStatuMovementModel;
import com.suse.lhw.hms.exersiseserver.dao.*;
import com.suse.lhw.hms.exersiseserver.domin.*;
import com.suse.lhw.hms.exersiseserver.pojo.out.MovementModelAndUnits;
import javafx.collections.SetChangeListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.suse.lhw.hms.exersiseserver.service.EMoventmodelService;
import org.springframework.web.bind.annotation.RequestBody;
import sun.management.snmp.jvmmib.EnumJvmThreadContentionMonitoring;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ProjectName: health_manage_system
 * @Package: com.suse.lhw.hms.exersiseserver.service.Impl
 * @ClassName: EMoventmodelServiceImpl
 * @Description: java类作用描述
 * @Author: 李鸿炜
 * @CreateDate: 2020/5/3 13:59
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/5/3 13:59
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Service
public class EMoventmodelServiceImpl implements EMoventmodelService {

    @Resource
    private EMoventmodelMapper eMoventmodelMapper;
    @Resource
    private EUnitMapper eUnitMapper;
    @Resource
    EMmToUnitMapper eMmToUnitMapper;
    @Resource
    EMmInstanceMapper eMmInstanceMapper;
    @Resource
    ECollectionMapper eCollectionMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {

        return eMoventmodelMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(EMoventmodel record) {
        return eMoventmodelMapper.insert(record);
    }

    @Override
    public int insertSelective(EMoventmodel record) {
        return eMoventmodelMapper.insertSelective(record);
    }

    @Override
    public EMoventmodel selectByPrimaryKey(Integer id) {
        return eMoventmodelMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(EMoventmodel record) {
        return eMoventmodelMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(EMoventmodel record) {
        return eMoventmodelMapper.updateByPrimaryKey(record);
    }

    @Override
    public int addAMovenmentModelByStatus(@RequestBody EMoventmodel eMoventmodel, @RequestBody List<EUnit> list) {
        //先增加食品模型
        System.out.println("emoventModel is" + eMoventmodel);
//        System.out.println("list is"+list.toString());
        if (eMoventmodelMapper.insertSelective(eMoventmodel) == 0)
            return 0;
        //再添加食品的单位以及单位映射
        int mmId = eMoventmodel.getId();
        for (EUnit eUnit : list) {
            if (eUnitMapper.insertSelective(eUnit) == 0)
                return 0;
            EMmToUnit eMmToUnit = new EMmToUnit(mmId, eUnit.getId());
            if (eMmToUnitMapper.insertSelective(eMmToUnit) == 0)
                return 0;
        }
        return 1;
    }

    @Override
    public int updateAMovenmentModelAndUnit(EMoventmodel eMoventmodel, List<EUnit> list) {
        //首先更新movementmodel
        if (eMoventmodelMapper.updateByPrimaryKeySelective(eMoventmodel) == 0)
            return 0;
        //更新单位列表
        for (EUnit eUnit : list) {
            if (eUnitMapper.updateByPrimaryKey(eUnit) == 0)
                return 0;
        }
        return 1;
    }

    @Override
    public List<MovementModelAndUnits> selectAllModelByStatus(int status) {
        //先找出所有的模型
        List<EMoventmodel> list = eMoventmodelMapper.selectAllByStatus(status);
        //通过没个emovenmentmodel的id进行添加
        List<MovementModelAndUnits> result = new ArrayList<MovementModelAndUnits>();
        for (EMoventmodel eMoventmodel : list) {
            List<EUnit> units = eUnitMapper.selectByMMID(eMoventmodel.getId());
            MovementModelAndUnits movementModelAndUnits = new MovementModelAndUnits(
                    eMoventmodel.getMmName(), eMoventmodel.getMmImgurl(),
                    eMoventmodel.getMmCategoryId(), eMoventmodel.getStatus(),
                    eMoventmodel.getMmPackagetype(), eMoventmodel.getMmDescription(),
                    eMoventmodel.getUserid(), eMoventmodel.getDifficulty(),
                    eMoventmodel.getVideourl(), eMoventmodel.getPoints(), units);
            result.add(movementModelAndUnits);
        }
        return result;
    }

    @Override
    public int excuteAMM(EUnit eUnit, EMoventmodel eMoventmodel, int quanlity, String userId) {
        //执行一个单一运动，将传入的模型和单位生成一个instatce
        EMmInstance instance = new EMmInstance();
        instance.setMmid(eMoventmodel.getId());
        instance.setQuanity(quanlity);
        instance.setUserid(userId);
        instance.setCreatetime(new Date());
        int cosume = eUnit.getConsumCalories() * quanlity;
        instance.setCalories(cosume);
//        instance.setType(cosume);
        instance.setType(ConstEMmInstance.enable);
        //增加instance
        if (eMmInstanceMapper.insertSelective(instance) == 0)
            return 0;
        return 1;
    }

    @Override
    public MovementModelAndUnits selectAModelById(int id) {
        EMoventmodel eMoventmodel = eMoventmodelMapper.selectByPrimaryKey(id);
        //根据模型id查找单位
        List<EUnit> list = eUnitMapper.selectByMMID(id);
        MovementModelAndUnits movementModelAndUnits = new MovementModelAndUnits();
        movementModelAndUnits.setDifficulty(eMoventmodel.getDifficulty());
        movementModelAndUnits.setEUnitList(list);
        movementModelAndUnits.setMmCategoryId(eMoventmodel.getMmCategoryId());
        movementModelAndUnits.setMmDescription(eMoventmodel.getMmDescription());
        movementModelAndUnits.setMmImgurl(eMoventmodel.getMmImgurl());
        movementModelAndUnits.setMmName(eMoventmodel.getMmName());
        movementModelAndUnits.setStatus(eMoventmodel.getStatus());
        return movementModelAndUnits;
    }

    @Override
    public List<MovementModelAndUnits> selectCollectedByUseridType(String userId, int type) {
        //通过type判断需要查找套餐还是模型
        //先找收藏
        List<ECollection> list = eCollectionMapper.selectByUserIdAndType(userId, type);
        if (list.isEmpty())
            return null;
        List<MovementModelAndUnits> movementModelAndUnits = new ArrayList<MovementModelAndUnits>();
        for (ECollection eCollection : list) {
            MovementModelAndUnits modelAndUnits = new MovementModelAndUnits();
            EMoventmodel eMoventmodel = eMoventmodelMapper.selectByPrimaryKey(eCollection.getCollectionid());
            List<EUnit> units = eUnitMapper.selectByMMID(eMoventmodel.getId());
            modelAndUnits.setId(eMoventmodel.getId());

            modelAndUnits.setStatus(eMoventmodel.getStatus());
            modelAndUnits.setMmName(eMoventmodel.getMmName());
            modelAndUnits.setMmImgurl(eMoventmodel.getMmImgurl());
            modelAndUnits.setMmCategoryId(eMoventmodel.getMmCategoryId());
            modelAndUnits.setDifficulty(eMoventmodel.getDifficulty());
            modelAndUnits.setMmDescription(eMoventmodel.getMmDescription());
            modelAndUnits.setUserid(eMoventmodel.getUserid());
            modelAndUnits.setVideourl(eMoventmodel.getVideourl());
            modelAndUnits.setPoints(eMoventmodel.getPoints());
            modelAndUnits.setEUnitList(units);
            movementModelAndUnits.add(modelAndUnits);
        }
        return movementModelAndUnits;
    }

    @Override
    public List<MovementModelAndUnits> selectAllByUseridAndStatusAndName(String userId, int type, String name) {
        List<MovementModelAndUnits> result = new ArrayList<MovementModelAndUnits>();
        type++;
        //如果传来的type是default，没有搜名字,则不需要userid
        try {


            if (type == ConstStatuMovementModel.defaultt && name == "") {
                List<EMoventmodel> mms = eMoventmodelMapper.selectAllByStatus(ConstStatuMovementModel.defaultt);
                for (EMoventmodel mm : mms) {
                    MovementModelAndUnits movementModelAndUnits = new MovementModelAndUnits();
                    List<EUnit> units = eUnitMapper.selectByMMID(mm.getId());
                    movementModelAndUnits.setId(mm.getId());
                    movementModelAndUnits.setEUnitList(units);
                    movementModelAndUnits.setMmName(mm.getMmName());
                    movementModelAndUnits.setMmImgurl(mm.getMmImgurl());
                    movementModelAndUnits.setMmCategoryId(mm.getMmCategoryId());
                    movementModelAndUnits.setDifficulty(mm.getDifficulty());
                    movementModelAndUnits.setMmDescription(mm.getMmDescription());
                    movementModelAndUnits.setUserid(userId);
                    movementModelAndUnits.setStatus(mm.getStatus());
                    movementModelAndUnits.setVideourl(mm.getVideourl());
                    movementModelAndUnits.setPoints(mm.getPoints());

                    result.add(movementModelAndUnits);
                }
            }
            //如果传来的type是default，用了搜索框则需要用name属性
            if (type == ConstStatuMovementModel.defaultt && name != "") {
                List<EMoventmodel> ems = eMoventmodelMapper.selectByNameAndType(name, ConstStatuMovementModel.defaultt);
                for (EMoventmodel mm : ems) {
                    MovementModelAndUnits movementModelAndUnits = new MovementModelAndUnits();
                    List<EUnit> units = eUnitMapper.selectByMMID(mm.getId());
                    movementModelAndUnits.setStatus(mm.getStatus());
                    movementModelAndUnits.setEUnitList(units);
                    movementModelAndUnits.setId(mm.getId());
                    movementModelAndUnits.setMmName(mm.getMmName());
                    movementModelAndUnits.setMmImgurl(mm.getMmImgurl());
                    movementModelAndUnits.setMmCategoryId(mm.getMmCategoryId());
                    movementModelAndUnits.setDifficulty(mm.getDifficulty());
                    movementModelAndUnits.setMmDescription(mm.getMmDescription());
                    movementModelAndUnits.setUserid(userId);
                    movementModelAndUnits.setVideourl(mm.getVideourl());
                    movementModelAndUnits.setPoints(mm.getPoints());

                    result.add(movementModelAndUnits);
                }
            }
            //传来的type是用户自定义的，没用搜索框
            if ((type == ConstStatuMovementModel.un_upload || type == ConstStatuMovementModel.uploaded) && name == "") {
                List<EMoventmodel> ems = eMoventmodelMapper.selectByUserIdAndType(userId, type);
                for (EMoventmodel mm : ems) {
                    MovementModelAndUnits movementModelAndUnits = new MovementModelAndUnits();
                    List<EUnit> units = eUnitMapper.selectByMMID(mm.getId());
                    movementModelAndUnits.setId(mm.getId());
                    movementModelAndUnits.setStatus(mm.getStatus());
                    movementModelAndUnits.setEUnitList(units);
                    movementModelAndUnits.setMmName(mm.getMmName());
                    movementModelAndUnits.setMmImgurl(mm.getMmImgurl());
                    movementModelAndUnits.setMmCategoryId(mm.getMmCategoryId());
                    movementModelAndUnits.setDifficulty(mm.getDifficulty());
                    movementModelAndUnits.setMmDescription(mm.getMmDescription());
                    movementModelAndUnits.setUserid(userId);
                    movementModelAndUnits.setVideourl(mm.getVideourl());
                    movementModelAndUnits.setPoints(mm.getPoints());

                    result.add(movementModelAndUnits);
                }

            }
            //传来的type是用户自定义的，且用了搜索框
            if ((type == ConstStatuMovementModel.un_upload || type == ConstStatuMovementModel.uploaded) && name != "") {
                List<EMoventmodel> ems = eMoventmodelMapper.selectByNameTypeUserid(name, type, userId);
                for (EMoventmodel mm : ems) {
                    MovementModelAndUnits movementModelAndUnits = new MovementModelAndUnits();
                    List<EUnit> units = eUnitMapper.selectByMMID(mm.getId());
                    movementModelAndUnits.setId(mm.getId());
                    movementModelAndUnits.setEUnitList(units);
                    movementModelAndUnits.setMmName(mm.getMmName());
                    movementModelAndUnits.setMmImgurl(mm.getMmImgurl());
                    movementModelAndUnits.setMmCategoryId(mm.getMmCategoryId());
                    movementModelAndUnits.setDifficulty(mm.getDifficulty());
                    movementModelAndUnits.setMmDescription(mm.getMmDescription());
                    movementModelAndUnits.setUserid(userId);
                    movementModelAndUnits.setVideourl(mm.getVideourl());
                    movementModelAndUnits.setPoints(mm.getPoints());

                    result.add(movementModelAndUnits);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        if (result.isEmpty() || result == null)
            return null;
        return result;
    }

    @Override
    public int deleteByModelId(Integer movId) {
        /**
         * @Author:Hongwei Li
         * @Description:删除一个运动模型，当然要连着运动模型，运动单位等一起删喽
         * @Date:18:12 2020/8/13
         * @Param:[movId]
         * @Return:int
         **/
        //先删除运动模型
        EMoventmodel eMoventmodel = eMoventmodelMapper.selectByPrimaryKey(movId);
        int modelDeleteResult = eMoventmodelMapper.deleteByPrimaryKey(movId);
        if (modelDeleteResult == 1) {
            //再删除运动单位和链接还有各种实例

            List<Integer> mapList = eMmToUnitMapper.selectPrimaryIdAllByMMId(movId);
            List<EUnit> unitList = eUnitMapper.selectByMMID(movId);
            List<Integer> integerList = eMmInstanceMapper.selectIdByMMID(movId);
            int a = 1;
            int b = 1;
            int c = 1;
            //链接
            for (Integer integer : mapList) {
                //&当且仅当两者都为1，才是1，如果循环中出现了一次失败（返回值为0），就会一直是0
                a &= eMmToUnitMapper.deleteByPrimaryKey(integer);
                System.out.println("a is " + a);
            }
            //单位

            for (EUnit eUnit : unitList) {
                b &= eUnitMapper.deleteByPrimaryKey(eUnit.getId());
                System.out.println("b is " + b);
            }
            //实例

            if (integerList != null)
                for (Integer integer : integerList) {
                    c &= eMmInstanceMapper.deleteByPrimaryKey(integer);
                    System.out.println("c is " + c);
                }
            System.out.println("a b c is " + (a & b & c));
            if ((a & b & c) == 1) {
                //全部都为1，就说明全部删除成功了
                return 1;
            } else
                return 0;
        }
        return 0;
    }
}


